package com.quanyue.music.system.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.quanyue.music.domain.constant.TimeZoneType;
import com.quanyue.music.domain.constant.UserRole;
import com.quanyue.music.domain.entity.CenterStatus;
import com.quanyue.music.domain.entity.IpBlock;
import com.quanyue.music.domain.entity.IsTest;
import com.quanyue.music.system.mapper.CenterStatusMapper;
import com.quanyue.music.system.mapper.IpBlockMapper;
import com.quanyue.music.system.mapper.IsTestMapper;
import com.quanyue.music.system.service.CenterService;
import com.quanyue.music.system.type.CenterShowResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "控制中心")
@RestController
@RequestMapping("/api")
public class CenterController {
    @Autowired
    private CenterService centerService;
    @Autowired
    private IsTestMapper isTestMapper;
    @Autowired
    private IpBlockMapper ipBlockMapper;
    @Autowired
    private CenterStatusMapper centerStatusMapper;

    @ApiOperation(value = "控制中心展示")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/center/show/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <CenterShowResult> showList() {
        return ServiceResult.success(centerService.findShowList());
    }

    @ApiOperation(value = "控制中心时间展示")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/center/zone/times", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Map <String, LocalTime>> showTimes() {
        Map <String, LocalTime> map = new HashMap <>();
        map.put(TimeZoneType.EST.toString(), LocalTime.now());
        map.put(TimeZoneType.CST.toString(), LocalTime.now().minusHours(1));
        map.put(TimeZoneType.MST.toString(), LocalTime.now().minusHours(2));
        map.put(TimeZoneType.PST.toString(), LocalTime.now().minusHours(3));
        map.put("BJT", LocalTime.now(ZoneId.of("Asia/Shanghai")));
        return ServiceResult.success(map);
    }

    @ApiOperation(value = "控制中心状态更新")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/center/status/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody CenterStatus centerStatuses) {
        centerService.updateStatus(centerStatuses);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询管理员点击的按钮")
    @GetMapping(value = "/center/status/find", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <CenterStatus>> findOnClick() {
        Boolean status = false;
        return ServiceResult.success(centerStatusMapper.findByStatus(status));
    }

    @ApiOperation(value = "添加")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/center/status/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> add(@RequestBody CenterStatus centerStatuses) {
        centerStatuses.setStatus(true);
        centerStatusMapper.insert(centerStatuses);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询是否开启测试模式")
    @GetMapping(value = "/center/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <IsTest> isTest() {
        return ServiceResult.success(isTestMapper.findAll().get(0));
    }

    @ApiOperation(value = "改变模式")
    @PostMapping(value = "/center/test/edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> editTest(@RequestBody IsTest isTest) {
        isTestMapper.update(isTest);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "查询是否开启ip拦截")
    @GetMapping(value = "/center/ipblock", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <IpBlock> ipBlock() {
        return ServiceResult.success(ipBlockMapper.findAll().get(0));
    }

    @ApiOperation(value = "修改拦截状态")
    @GetMapping(value = "/center/ipblock/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> editIpBlock(@RequestParam Boolean isOpen) {
        IpBlock ipBlock = ipBlockMapper.findAll().get(0);
        ipBlock.setIsOpen(isOpen);
        ipBlock.setModifyTime(LocalDateTime.now());
        ipBlockMapper.update(ipBlock);
        return ServiceResult.SUCCESS;
    }
}
